<template>
  <div class="Dialog">
    <el-dialog title="新增员工" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off" />
        </el-form-item>
        <el-form-item label="入职时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="聘用形式" :label-width="formLabelWidth">
          <el-select
            v-model="form.formOfEmployment"
            placeholder="请选择活动区域"
          >
            <el-option
              v-for="item in hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" :label-width="formLabelWidth">
          <el-input v-model="form.workNumber" autocomplete="off" />
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth">
          <el-input
            v-model="form.departmentName"
            autocomplete="off"
            @focus="isShowTree = true"
          />
          <el-tree
            v-show="isShowTree"
            :data="departmentData"
            :props="defaultProps"
            default-expand-all
            icon-class=" "
            @node-click="handleNodeClick"
          />
        </el-form-item>
        <el-form-item label="转正时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="getForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入部门接口
import { getDepartments } from '@/api/departments'

// 导入辅助函数（转树方法）
import { formatData } from '@/utils'

// 导入 employees 枚举
import EMPLOYEECON from '@/api/constant/employees'

// 导入新增员工接口
import { addEmployee } from '@/api/employees.js'

export default {
  data() {
    return {
      dialogFormVisible: false, // 弹框状态
      formLabelWidth: '120px',
      isShowTree: false,
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      departmentData: [], // 部门数组
      hireType: EMPLOYEECON.hireType, // 聘用形式数组
      // 修改tree组件查找数据属性的对应的key
      defaultProps: {
        children: 'children',
        label: 'name' // 文本
      }
    }
  },
  async created() {
    const res = await getDepartments()
    this.departmentData = formatData(res.depts, '')
  },
  methods: {
    // 提交表单
    async getForm() {
      const res = await addEmployee(this.form)
      // console.log(res)
      // 获取父组件
      this.$parent.getData()
      // 关闭弹框
      this.dialogFormVisible = false
      // 清空表单
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 提示
      this.$message({
        message: '新增成功',
        type: 'success'
      })
    },
    handleNodeClick(data) {
      this.form.departmentName = data.name
      this.isShowTree = false
    }
  }
}
</script>

<style lang="scss">
.Dialog {
  .el-form-item__content {
    width: 320px;
  }
  .el-tree {
    border: 1px solid #dcdfe6;
    position: absolute;
    width: 320px;
    z-index: 999;
  }
}
</style>
